package com.iran.observer.test;

import com.iran.observer.ConcreteObserver;
import com.iran.observer.ConcreteSubject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Observer;
import java.util.Set;

public class test {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        //构建一个具体的主题对象
        ConcreteSubject subject = new ConcreteSubject();
        //创建多个观察者对象
        ConcreteObserver c1 = new ConcreteObserver();
        //Observer o = (Observer)Class.forName("com.iran.observer.ConcreteObserver").newInstance();

        //System.out.println(o.getClass().getCanonicalName());//com.iran.observer.ConcreteObserver
        c1.setMessage("c1");
        ConcreteObserver c2 = new ConcreteObserver();
        c2.setMessage("c2");
        ConcreteObserver c3 = new ConcreteObserver();
        c3.setMessage("c3");
        subject.addObserver(c1);//Observer接口中使用addObserver来添加观察者对象到集合中
        subject.addObserver(c2);
        subject.addObserver(c3);
        //查看未修改时的状态
        System.out.println(c1.getMessage());
        System.out.println(c2.getMessage());
        System.out.println(c3.getMessage());

        System.out.println("-------------修改后的状态-------------");
        //改变目标对象的状态
//      subject.setState(1000);
        subject.publishMsg("消息改变了。你们都变成CCCCCC");//使用刚才定义的updateState方法来更新状态
        //查看观察者对象的状态
        System.out.println(c1.getMessage());
        System.out.println(c2.getMessage());
        System.out.println(c3.getMessage());

        HashMap<String, String> map = new HashMap<>();
        Set<String> strings = map.keySet();
        Iterator<String> iterator = strings.iterator();

    }
}
